<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="房间号" prop="roomNum">
        <el-input
          v-model="queryParams.roomNum"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房客姓名" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入房客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间状态" prop="status">
        <el-input
          v-model="queryParams.status"
          placeholder="请输入房间状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入房间类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出Excel</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getRoomList"></right-toolbar>
    </el-row>


    <!--表格数据-->
    <el-table stripe v-loading="loading" :data="pageList" @selection-change="handleSelectionChange">
      <el-table-column label="房间号" align="center" prop="roomNum" />
      <el-table-column label="房源昵称" align="center" prop="apartmentName" width="200px"/>
      <el-table-column label="房客姓名" align="center" prop="tenantName" :show-overflow-tooltip="true"/>
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="租金" align="center" prop="rent" />
      <el-table-column label="房间状态" align="center" prop="status" />
      <el-table-column label="房间类型" align="center" prop="type" />
      <el-table-column label="房间面积 m²" align="center" prop="area" />
      <el-table-column label="家具" align="center" prop="furniture" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getRoomList"
    />

  </div>
</template>

<script>
import {addRoomApi, delRoomApi, getRoomApi, listRoomApi, updateRoomApi} from "@/api/apartment/room";

export default {
    data() {
      return {
        // 表单参数
        form: {},
        // 加载遮罩
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 房间数据
        pageList: [],
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单label宽度
        formLabelWidth: '100px',
        // 房间编辑models
        editFrom:{},
        // 房间状态
        roomState: '',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          id: null,
          roomNum: null,
          apartmentName: null,
          tenantName: null,
          phone: null,
          rent: null,
          status: null,
          type: null,
          area: null,
          furniture: null,
          createTime: null
        },
        // 表单校验
        rules: {
          apartment_name: [
            { required: true, message: "公寓名称不能为空", trigger: "blur" }
          ],
          address: [
            { required: true, message: "公寓地址不能为空", trigger: "blur" }
          ]
        }
      }
    },
  mounted() {
      this.getRoomList();
  },

  methods: {

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getRoomList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.getRoomList();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        apartment_name: null,
        owner: null,
        address: null,
        state: null,
        create_time: null
      };
      this.resetForm("form");
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作: 新增房源 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公寓信息";
    },

    /** 修改按钮操作: 修改房源 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRoomApi(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公寓信息";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRoomApi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getHouseList();
            });
          } else {
            addRoomApi(this.form).then(res => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getRoomList();
            });
          }
        }
      });
    },

    /** 删除按钮操作: 删除指定房源 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除房间信息编号为"' + ids + '"的数据项？').then(function() {
        return delRoomApi(ids);
      }).then(() => {
        this.getRoomList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        this.$modal.msgError("网络异常");
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('apartment/room/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },

    /** 查询所有房间 */
    getRoomList() {
      this.loading = true;
      listRoomApi(this.queryParams).then(res =>{
        this.pageList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },

  }
  }
</script>

<style>

</style>
